package com.sxm.yiwei.pinche;

import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.R;
import com.sxm.yiwei.base.BaseActivity;
import com.sxm.yiwei.beans.pinche.HostOrder;
import com.sxm.yiwei.constant.Common;
import com.sxm.yiwei.constant.Constant;
import com.sxm.yiwei.tools.BookPayActivity;
import com.sxm.yiwei.views.AmountView;
import com.sxm.yiwei.views.CenterDialog;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/10/27.
 */

public class HostOrderDetailActivity extends BaseActivity implements CenterDialog.OnCenterItemClickListener {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView tv_style;
    private TextView tv_distance;
    private TextView tv_rest_seats;
    private TextView tv_single_price;
    private TextView tv_depart_time;
    private TextView tv_start_pos;
    private TextView tv_end_pos;
    private AmountView amount_view;
    private TextView tv_phone;
    private TextView tv_words;
    private ImageView iv_icon;
    private TextView tv_nickname;
    private TextView tv_recent_come;
    private TextView tv_wei;
    private TextView tv_pay_money;
    private TextView tv_buy;
    private ImageView img1;
    private ImageView img2;
    private ImageView img3;
    private ImageView img4;
    private ProgressDialog progressDialog;
    private CenterDialog centerDialog;
    private int failure_code;
    private HostOrder hostOrder;
    private EditText etAmount;
    private double total_price;
    private double single_price;
    private int seats_require=1;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_order_detail_host);
    }

    @Override
    public void initView() {
        rl_back = (RelativeLayout)findViewById(R.id.rl_back);
        tv_title = (TextView)findViewById(R.id.tv_title);
        tv_style = (TextView)findViewById(R.id.tv_style);
        tv_distance = (TextView)findViewById(R.id.tv_distance);
        tv_rest_seats = (TextView)findViewById(R.id.tv_rest_seats);
        tv_single_price = (TextView)findViewById(R.id.tv_single_price);
        tv_depart_time = (TextView)findViewById(R.id.tv_depart_time);
        tv_start_pos = (TextView)findViewById(R.id.tv_start_pos);
        tv_end_pos = (TextView)findViewById(R.id.tv_end_pos);
        amount_view = (AmountView)findViewById(R.id.amount_view);
        etAmount = (EditText)amount_view.findViewById(R.id.etAmount);
        tv_phone = (TextView)findViewById(R.id.tv_phone);
        tv_words = (TextView)findViewById(R.id.tv_words);
        iv_icon = (ImageView)findViewById(R.id.iv_icon);
        tv_nickname = (TextView)findViewById(R.id.tv_nickname);
        tv_recent_come = (TextView)findViewById(R.id.tv_recent_come);
        tv_wei = (TextView)findViewById(R.id.tv_wei);
        tv_pay_money = (TextView)findViewById(R.id.tv_pay_money);
        tv_buy = (TextView)findViewById(R.id.tv_buy);
        img1 = (ImageView)findViewById(R.id.img1);
        img2 = (ImageView)findViewById(R.id.img2);
        img3 = (ImageView)findViewById(R.id.img3);
        img4 = (ImageView)findViewById(R.id.img4);
    }

    @Override
    public void initListener() {
        rl_back.setOnClickListener(this);
        tv_wei.setOnClickListener(this);
        tv_buy.setOnClickListener(this);
        amount_view.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, int amount) {
                seats_require=amount;
                total_price=seats_require*single_price;
                tv_pay_money.setText("需支付"+total_price+"元");
                if (amount==hostOrder.getSeats()){
                    showToast("已到达最大座位数");
                }
            }
        });
    }

    @Override
    public void initData() {
        tv_title.setText("订单详情");
        hostOrder = (HostOrder) getIntent().getSerializableExtra("hostOrder");
        single_price=hostOrder.getPrice();
        total_price=single_price;
        switch (hostOrder.getStyle()){
            case Constant.LONG:
                tv_style.setText("长途");
                break;
            case Constant.CITY:
                tv_style.setText("同城");
                break;
            case Constant.TRAVEL:
                tv_style.setText("旅游");
                break;
            case Constant.LONG_HURRY:
                tv_style.setText("长途急件");
                break;
        }
        amount_view.setGoods_storage(hostOrder.getSeats());
        etAmount.setEnabled(false);
        tv_distance.setText(hostOrder.getDistance()+"公里");
        tv_rest_seats.setText("剩余"+hostOrder.getSeats()+"个座位");
        tv_single_price.setText(hostOrder.getPrice()+"元");
        tv_depart_time.setText(hostOrder.getDepart_time());
        tv_start_pos.setText(hostOrder.getStart_place());
        tv_end_pos.setText(hostOrder.getEnd_place());
        String phone = hostOrder.getPhone();
        phone=phone.substring(0,3)+"****"+phone.substring(7,11);
        tv_phone.setText(phone);
        tv_words.setText(hostOrder.getWords());
        x.image().bind(iv_icon,Common.IMAGE_HOST+hostOrder.getIcon());
        tv_nickname.setText(hostOrder.getNickname());
        tv_pay_money.setText("需支付"+total_price+"元");
        ArrayList<String> imgList = hostOrder.getImgList();
        switch (imgList.size()){
            case 0:
                break;
            case 1:
                x.image().bind(img1,Common.IMAGE_HOST+imgList.get(0));
                break;
            case 2:
                x.image().bind(img1,Common.IMAGE_HOST+imgList.get(0));
                x.image().bind(img2,Common.IMAGE_HOST+imgList.get(1));
                break;
            case 3:
                x.image().bind(img1,Common.IMAGE_HOST+imgList.get(0));
                x.image().bind(img2,Common.IMAGE_HOST+imgList.get(1));
                x.image().bind(img3,Common.IMAGE_HOST+imgList.get(2));
                break;
            case 4:
                x.image().bind(img1,Common.IMAGE_HOST+imgList.get(0));
                x.image().bind(img2,Common.IMAGE_HOST+imgList.get(1));
                x.image().bind(img3,Common.IMAGE_HOST+imgList.get(2));
                x.image().bind(img4,Common.IMAGE_HOST+imgList.get(3));
                break;
        }

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.tv_wei:
                showToast("暂未开放");
                break;
            case R.id.tv_buy:
                JSONObject object = new JSONObject();
                object.put("serviceId",hostOrder.getService_id());//服务主键
                object.put("ownerId",hostOrder.getOwner_id());//车主主键
                object.put("passerId",getHeadInfo().getUserId());//乘客主键
                object.put("passerRequireSeats",seats_require+"");//座位
                object.put("pinTotalAccount",total_price+"");//订单发布金额
                httpPost(Common.HOST+Common.PASSENGER_BUY_TICKET,object,"1");
                break;
        }

    }

    @Override
    public void OnCenterItemClick(CenterDialog dialog, View view) {

    }


    @Override
    public void onSuccess(String result, String fromWhere) {
        super.onSuccess(result, fromWhere);
        com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
        JSONObject data = obj.getJSONObject("data");
        if (fromWhere.equals("1")){
            Intent intent = new Intent(this, BookPayActivity.class);
            intent.putExtra("orderCode",data.getString("orderId"));//orderId
            intent.putExtra("amount",total_price+"");//支付总金额
            intent.putExtra("from", Constant.PASSENGER_ROB_PAY);
            intent.putExtra("id",hostOrder.getService_id());
            intent.putExtra("alipay",Common.PASSENGER_SEARCH_ALIPAY);
            intent.putExtra("yivipay",Common.PASSENGER_SEARCH_RESTPAY);
            intent.putExtra("type",Constant.PASSENGER_ROB_OK);
            intent.putExtra("text_1",hostOrder.getDepart_time());
            intent.putExtra("text_2",data.getString("orderCode"));
            Log.i("hostOrderDetail","type="+Constant.PASSENGER_ROB_OK+"text1="+hostOrder.getDepart_time()+"text2"+data.getString("orderCode"));
            startActivity(intent);

        }

    }

    @Override
    public void onSuccessError(String result, String fromWhere) {
        super.onSuccessError(result, fromWhere);
    }
}
